package com.yeu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.yeu.util.ConnectionPool;

import java.util.Map;
import java.util.List;
import java.text.DateFormat;
import java.util.Date;
import java.util.Calendar;
import org.apache.struts2.interceptor.SessionAware;
public class EventEditAction extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private String reason;

	private String eventId;
	private String editBy;
	private String editTime;
	private String operation;
	private String oldValue;
	private String newValue;
	private Map<String,Boolean> boolResult;
	private List<Map<String,String>> result;
	private Map<String,Object> session;

	private static final long serialVersionUID = 1L;
	public EventEditAction(){
		super();
	}
	
	public String getOldValue() {
		return oldValue;
	}

	public void setOldValue(String oldValue) {
		this.oldValue = oldValue;
	}

	public String getNewValue() {
		return newValue;
	}

	public void setNewValue(String newValue) {
		this.newValue = newValue;
	}

	private void setUserAndTime(){
		editBy = (String)session.get("userName");
		//System.out.println("username = " + editBy);
		DateFormat dfmt = DateFormat.getDateTimeInstance();
		editTime = dfmt.format(Calendar.getInstance().getTime());
		//System.out.println("editTime = " + editTime);
	}
	
	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public List<Map<String, String>> getResult() {
		return result;
	}

	public void setResult(List<Map<String, String>> result) {
		this.result = result;
	}
	
	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}
	
	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getEditBy() {
		return editBy;
	}

	public void setEditBy(String editBy) {
		this.editBy = editBy;
	}

	public String getEditTime() {
		return editTime;
	}

	public void setEditTime(String editTime) {
		this.editTime = editTime;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}

	public String saveReason(){
		setUserAndTime();
		String sqlQuery = "select * from EVENTEDITSTATUS where EVENTID = ?";
		result = ConnectionPool.executeQuery(sqlQuery, eventId);
		if(result.size() == 0){
			String sql = "insert into EVENTEDITSTATUS(EVENTID,EVENTEDITREASON,EVENTEDITOPERATION,EVENTEDITBY,EVENTEDITTIME) values(?,?,?,?,?)";
			boolResult = ConnectionPool.executeUpdate(sql,eventId,reason,operation,editBy,editTime);
		} else{
			String sql = "update EVENTEDITSTATUS set EVENTEDITREASON = ?,EVENTEDITOPERATION=?, EVENTEDITBY=?,EVENTEDITTIME=? where eventId=?";
			boolResult = ConnectionPool.executeUpdate(sql,reason,operation,editBy,editTime,eventId);
		}
		
		//System.out.println(boolResult);
		return "boolResult";
	}
	
	public String getReason(){
		String sql = "select EVENTEDITREASON from EVENTEDITSTATUS where EVENTID = ?";
		result = ConnectionPool.executeQuery(sql,eventId);
		return "success";
	}
	
	public String EditStatusOp(){
		setUserAndTime();
		//System.out.println("oldValue="+oldValue+",newValue="+newValue);
		String sql = "insert into EVENTEDITSTATUS(EVENTID,EVENTEDITBY,EVENTEDITTIME,EVENTEDITREASON,EVENTEDITOPERATION,OLDVALUE,NEWVALUE) values(?,?,?,?,?,?,?)";
		boolResult = ConnectionPool.executeUpdate(sql, eventId,editBy,editTime,reason,operation,oldValue,newValue);
		return "boolResult";
	}
	
	public String EditAdminDepartmentOp(){
		setUserAndTime();
		//System.out.println("oldValue="+oldValue+",newValue="+newValue);
		String sql = "insert into EVENTEDITSTATUS(EVENTID,EVENTEDITBY,EVENTEDITTIME,EVENTEDITREASON,EVENTEDITOPERATION,OLDVALUE,NEWVALUE) values(?,?,?,?,?,?,?)";
		boolResult = ConnectionPool.executeUpdate(sql, eventId,editBy,editTime,reason,operation,oldValue,newValue);
		return "boolResult";
	}
	
	public String EditEventTypeOp(){
		setUserAndTime();
		//System.out.println("oldValue="+oldValue+",newValue="+newValue);
		String sql = "insert into EVENTEDITSTATUS(EVENTID,EVENTEDITBY,EVENTEDITTIME,EVENTEDITREASON,EVENTEDITOPERATION,OLDVALUE,NEWVALUE) values(?,?,?,?,?,?,?)";
		boolResult = ConnectionPool.executeUpdate(sql, eventId,editBy,editTime,reason,operation,oldValue,newValue);
		return "boolResult";
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session = arg0;
	}
	public Map<String,Object>getSession(){
		return this.session;
	}
	
}
